﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;
namespace RooHui.OA.Biz
{
    public class PromotPolicyBiz
    {
        /// <summary>
        /// 获取所有促销方案信息
        /// </summary>
        /// <returns></returns>
        public static List<PromotionPolicy> GetPromotPolicyList()
        {
            try
            {
                var promotPolicyList = new PromotionPolicy().FindAll();
                return promotPolicyList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 分页获取促销方案信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<PromotionPolicy> GetPromotPolicyListByPage(int page, int pageSize, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var promotionPolicyList = (from c in ctx.PromotionPolicy orderby c.ID select c);
                    count = promotionPolicyList.Count();
                    var data = promotionPolicyList
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new PromotionPolicy().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 增加促销方案
        /// </summary>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="enable"></param>
        /// <param name="activeCode"></param>
        /// <param name="attachPolicy"></param>
        /// <param name="createUser"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public static bool PromotPolicyAdd(DateTime beginDate, DateTime endDate, bool enable, int activeCode, String attachPolicy, int range)
        {
            try
            {
                var promotPolicy = new PromotionPolicy()
                {
                    BeginDate = beginDate,
                    CreateUser = System.Web.HttpContext.Current.User.Identity.Name,
                    ActiveCode = activeCode,
                    AttachPolicy = attachPolicy,
                    Enable = enable,
                    EndDate = endDate,
                    Range = (PromotionRange)range,
                    CreateTime = DateTime.Now
                };
                return promotPolicy.PostAdd() != null;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
        }

        /// <summary>
        /// 根据ID获取促销方案信息
        /// </summary>
        /// <param name="policyId"></param>
        /// <returns></returns>
        public static PromotionPolicy GetPromotPolicyByID(int promotPolicyId)
        {
            try
            {
                return new PromotionPolicy().Find(promotPolicyId.ToString());
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 修改促销方案信息
        /// </summary>
        /// <param name="promotPolicyId"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="enable"></param>
        /// <param name="activeCode"></param>
        /// <param name="attachPolicy"></param>
        /// <param name="createUser"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public static bool PromotPolicyEdit(int promotPolicyId, DateTime beginDate, DateTime endDate, bool enable, int activeCode, String attachPolicy, int range)
        {
            try
            {
                var promotPolicy = new PromotionPolicy().Find(promotPolicyId.ToString());
                if (promotPolicy != null)
                {
                    promotPolicy.CreateUser = System.Web.HttpContext.Current.User.Identity.Name;
                    promotPolicy.ActiveCode = activeCode;
                    promotPolicy.CreateTime = DateTime.Now;
                    promotPolicy.AttachPolicy = attachPolicy;
                    promotPolicy.BeginDate = beginDate;
                    promotPolicy.Enable = enable;
                    promotPolicy.EndDate = endDate;
                    promotPolicy.Range = (PromotionRange)range;
                    return promotPolicy.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }

        /// <summary>
        /// 删除促销方案
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool PromotPolicyDelete(int id = 0)
        {
            try
            {
                var promotPolicy = new PromotionPolicy().Find(id.ToString());
                if (promotPolicy != null)
                {
                    return promotPolicy.PostDelete() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }
    }
}
